#pragma once

#include "noncopyable.h"
#include "Thread.h"

#include <mutex>
#include <condition_variable>
#include <functional>
#include <string>

class EventLoop;

// 创建一个新线程，新线程创建自己的EventLoop。存储该线程和EventLoop的对应关系
class EventLoopThread : noncopyable
{
public:
    using ThreadInitCallback = std::function<void(EventLoop *)>;

    EventLoopThread(const ThreadInitCallback &cb = ThreadInitCallback(), const std::string &name = std::string());
    ~EventLoopThread();

    //创建eventloop，并返回其地址
    EventLoop* startLoop();

private:
    void threadFunc();

    // 该线程的loop
    EventLoop *loop_;
    bool exiting_;
    Thread thread_;
    std::mutex mutex_;
    std::condition_variable cond_;
    //subloop的初始化函数
    ThreadInitCallback callback_;
};